#include "mywidget.h"
#include "ui_mywidget.h"

#include <QTimer>

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);

    // 开启定时器 方式1
    this->t1 = startTimer(1000);
    this->t2 = startTimer(2000);

    // 方式2开启定时器 (推荐)
    QTimer *timer = new QTimer(this);
    // 启动
    timer->start(500);
    // 处理信号
    connect(timer, &QTimer::timeout, [=]() {
        static int sec = 1;
        ui->label_3->setText(QString::number(sec++));
    });

    // 点击按钮暂停
    connect(ui->btn, &QPushButton::clicked, [=]() {
        timer->stop();
    });



}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::timerEvent(QTimerEvent *ev) {

    if (ev->timerId() == this->t1) {
        static int sec = 1;
        QString text = QString::number(sec++);
        ui->label->setText(text);

    } else if (ev->timerId() == this->t2) {
        static int sec = 1;
        QString text = QString::number(sec++);
        ui->label_2->setText(text);
    }

}
